#!/bin/bash
#
# kopano-monitor Kopano Core Quota Monitor
#
# chkconfig: 345 86 24
# processname: /usr/sbin/kopano-monitor
# config: /etc/kopano/monitor.cfg
# pidfile: /var/run/kopano/monitor.pid

### BEGIN INIT INFO
# Provides: kopano-monitor
# Required-Start: $local_fs $network $remote_fs $syslog
# Required-Stop: $local_fs $network $remote_fs $syslog
# Should-Start: kopano-server
# Should-Stop: kopano-server
# Short-Description: Kopano Core Quota Monitor
### END INIT INFO

MONITORCONFIG=/etc/kopano/monitor.cfg
MONITORPROGRAM=/usr/sbin/kopano-monitor

# Sanity checks.
[ -x $MONITORPROGRAM ] || exit 0

MONITORCONFIG_OPT=""
[ ! -z $MONITORCONFIG -a -f $MONITORCONFIG ] && MONITORCONFIG_OPT="-c $MONITORCONFIG"

[ -f /etc/sysconfig/kopano ] && . /etc/sysconfig/kopano
if [ -z "$KOPANO_LOCALE" ]; then
	KOPANO_LOCALE="C"
fi

# Source function library.
. /etc/rc.d/init.d/functions

RETVAL=0
monitor=`basename $MONITORPROGRAM`
lockfile=/var/lock/subsys/$monitor
pidfile=/var/run/kopano/monitor.pid

start() {
	# Start in background, always succeeds
	echo -n $"Starting $monitor: "
	install -dm0775 -o kopano -g kopano /var/run/kopano
	export LC_ALL=$KOPANO_LOCALE
	export LANG=$KOPANO_LOCALE
	daemon $MONITORPROGRAM $MONITORCONFIG_OPT
	RETVAL=$?
	unset LC_ALL LANG
	echo
	[ $RETVAL -eq 0 ] && touch $lockfile

	return $RETVAL
}

stop() {
	echo -n $"Stopping $monitor: "
	killproc $MONITORPROGRAM
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f $lockfile $pidfile

	return $RETVAL
}

restart() {
	stop
	start
}

reload() {
	echo -n $"Restarting $monitor: "
	killproc $MONITORPROGRAM -SIGHUP
	RETVAL=$?
	echo

	return $RETVAL
}

# See how we were called.
case "$1" in
    start)
		start
		;;
    stop)
		stop
		;;
    status)
		status $monitor
		RETVAL=$?
		;;
    restart|force-reload)
		restart
		;;
    condrestart|try-restart)
		if [ -f ${pidfile} ]; then
			stop
			start
		fi
		;;
    reload)
		reload
		;;
    *)
		echo $"Usage: $monitor {start|stop|status|reload|restart|condrestart|force-reload|try-restart}"
		RETVAL=1
		;;
esac

exit $RETVAL
